//
//  SettingView.swift
//  SwiftUI-Pokemaster
//
//  Created by gree on 2020/9/29.
//

import SwiftUI
import Combine

struct SettingView: View {
    @EnvironmentObject var store: Store
    var settingsBinding: Binding<AppState.Settings> {
        $store.appState.settings
    }
    
    var settings: AppState.Settings {
        store.appState.settings
    }
    
    var body: some View {
        Form {
            accountSection
            optionalSection
            actionSection
        }
        .alert(item: settingsBinding.loginError) { (error) -> Alert in
            Alert(title: Text(error.localizedDescription))
        }
    }
}

struct SettingView_Previews: PreviewProvider {
    static var previews: some View {
        SettingView().environmentObject(Store())
    }
}

extension AppState.Settings.Sorting {
    var text: String {
        switch self {
            case .id:
                return "ID"
            case .name:
                return "名字"
            case .color:
                return "颜色"
            case .favorite:
                return "最爱"
        }
    }
}

extension SettingView {
    
    var accountSection: some View {
        Section(header: Text("账户")) {
            if settings.loginUser == nil {
                Picker(selection: settingsBinding.accountBehavior, label: Text("Picker"), content: /*@START_MENU_TOKEN@*/{

                    ForEach(AppState.Settings.AccountBehavior.allCases, id: \.self) {
                        Text($0.text)
                    }
                }/*@END_MENU_TOKEN@*/)
                .pickerStyle(SegmentedPickerStyle())
                
                TextField("电子邮箱", text: settingsBinding.email)
                SecureField("密码", text: settingsBinding.password)
                
                if settings.accountBehavior == .register {
                    SecureField("确认密码", text: settingsBinding.verifyPassword)
                }
                if settings.loginRequesting {
                    Text("登录中...")
                } else {
                    Button(settings.accountBehavior.text) {
                        self.store.dispatch(
                            .login(email: self.settings.email, password: self.settings.password)
                        )
                    }
                }
                
            } else {
                Text(settings.loginUser!.email)
                Button("注销"){
                    print("注销")
                }
                
            }
        }
        
    }
}

extension SettingView {
    
    var optionalSection: some View {
        Section(header: Text("选项")) {
            Toggle(isOn: settingsBinding.showEnglishName) {
                Text("显示英文名")
            }
            Picker(
                selection: settingsBinding.sorting,
                label: Text("排列方式"))
            {
                ForEach(AppState.Settings.Sorting.allCases, id: \.self) {
                    Text($0.text)
                }
            }
            Toggle(isOn: settingsBinding.showFavoriteOnly) {
                Text("只显示收藏")
            }
        }
    }
}

extension SettingView {
    
    var actionSection: some View {
        Section() {
            Button(action: {}, label: {
                Text("清除缓存")
                    .font(.subheadline)
                    .foregroundColor(.red)
            })
        }
    }
}
